// var i;
// var s;
// var c;
// var l;
// var u;
// var AL_GAME_HUA_Tipsnode = require("./AL_GAME_HUA_Tipsnode");
// var f = cc._decorator;
// var d = f.ccclass;
// var h = f.property;
// (u = s || (s = {}))[(u.NONE = 0)] = "NONE";
// u[(u.SCALE = 1)] = "SCALE";
// (l = c || (c = {}))[(l.CIRCLE = 0)] = "CIRCLE";
// l[(l.RECT = 1)] = "RECT";
// l[(l.POLYGON = 2)] = "POLYGON";
// var _ = (function () {
//     function e(e, t, n) {
//         this.x = e;
//         this.y = t;
//         this.radius = n;
//     }
//     e.prototype.suofang_dongzuo = function (e, t) {
//         var n = this.radius * t;
//         e.drawCircle(this.x, this.y, cc.v2(n, n));
//     };
//     return e;
// })();

// function P(e, t, n) {
//     return new _(e, t, n);
// }
// var g = (function () {
//     function e(e, t, n, o, i) {
//         this.mx = e;
//         this.my = t;
//         this.round = i;
//         this.width = n;
//         this.height = o;
//         this.x = e + n / 2;
//         this.y = t + o / 2;
//     }
//     e.prototype.suofang_dongzuo = function (e, t) {
//         var n = this.width * t;
//         var o = this.height * t;
//         var i = this.x - n / 2;
//         var r = this.y - o / 2;
//         e.drawRoundRect(i, r, n, o, this.round);
//     };
//     e.prototype.getPolygon = function () {
//         return [
//             cc.v2(this.mx, this.my),
//             cc.v2(this.mx, this.my + this.height),
//             cc.v2(this.mx + this.width, this.my + this.height),
//             cc.v2(this.mx + this.width, this.my)
//         ];
//     };
//     return e;
// })();

// function y(e, t, n, o, i) {
//     if (void 0 === i) {
//         i = 0;
//     }
//     return new g(e, t, n, o, i);
// }
// var m = (function (e) {
//     function t() {
//         var t = (null !== e && e.apply(this, arguments)) || this;
//         t.node_finger = null;
//         t.node_mask = null;
//         t.tips_node = null;
//         t._node_target = null;
//         t._array_shape_list = [];
//         t._node_callback = null;
//         t._touchMoveEnabel = !1;
//         t._trigger_callback = null;
//         t._touch_bg_callback = null;
//         t._has_touch_start = !1;
//         t.mask = null;
//         t._effect_action = null;
//         return t;
//     }
//     __extends(t, e);
//     t.prototype.onLoad = function () {
//         this.mask = this.node_mask.getComponent(cc.Mask);
//         this.mask._updateGraphics = function () {};
//         this.mask.node.targetOff(this.mask);
//         this.mask.clearDraw();
//         this.tips_node.node.active = !1;
//         this.node_finger.active = !1;
//         this.node.on("touchstart", this.handle_event_touch_start, this);
//         this.node.on("touchmove", this.handle_event_touch_move, this);
//         this.node.on("touchend", this.handle_event_touch_end, this);
//     };
//     t.prototype.onDestroy = function () {
//         this.node.off("touchstart", this.handle_event_touch_start, this);
//         this.node.off("touchmove", this.handle_event_touch_move, this);
//         this.node.off("touchend", this.handle_event_touch_end, this);
//     };
//     t.prototype.jiancha_chumo_shijian = function (e) {
//         if (!this._touch_bg_callback) {
//             if (this._node_target) {
//                 this._node_target._hitTest(e.getLocation())
//                     ? this.node.setSwallowTouches(!1)
//                     : this.node.setSwallowTouches(!0);
//             } else {
//                 {
//                     if (this._array_shape_list.length > 0) {
//                         var t = 0;
//                         for (var n = this._array_shape_list; t < n.length; t++) {
//                             var o = n[t];
//                             if (o.enableTouch) {
//                                 var i = this.node_mask.convertToNodeSpaceAR(e.getLocation());
//                                 if (o.type === c.RECT) {
//                                     var r = o.shape;
//                                     if (cc.Intersection.pointInPolygon(i, r.getPolygon())) {
//                                         return void this.node.setSwallowTouches(!1);
//                                     }
//                                 } else {
//                                     if (o.type === c.CIRCLE) {
//                                         var a = o.shape;
//                                         if (i.sub(cc.v2(a.x, a.y)).magSqr() <= a.radius * a.radius) {
//                                             return void this.node.setSwallowTouches(!1);
//                                         }
//                                     }
//                                 }
//                             }
//                         }
//                     }
//                     this.node.setSwallowTouches(!0);
//                 }
//             }
//         }
//     };
//     t.prototype.handle_event_touch_start = function (e) {
//         this._has_touch_start = !0;
//         this.jiancha_chumo_shijian(e);
//     };
//     t.prototype.handle_event_touch_move = function (e) {
//         this._has_touch_start;
//         this._touchMoveEnabel ? this.jiancha_chumo_shijian(e) : this.node.setSwallowTouches(!0);
//     };
//     t.prototype.handle_event_touch_end = function (e) {
//         if (this._has_touch_start) {
//             if (this._touch_bg_callback) {
//                 this._touch_bg_callback();
//             }
//             this.jiancha_chumo_shijian(e);
//         }
//         this._has_touch_start = !1;
//     };
//     t.prototype.clear = function () {
//         if (this.node_finger) {
//             this.node_finger.active = !1;
//         }
//         this.node.setSwallowTouches(!0);
//         this._node_target = null;
//         this._array_shape_list = [];
//         this._touch_bg_callback = null;
//         this.node_mask.active = !1;
//         this.tips_node.node.active = !1;
//         this._has_touch_start = !1;
//     };
//     t.prototype.xianshi_node_tips = function (e) {
//         this.tips_node.node.active = !0;
//         this.tips_node.node.position = cc.v3(e.x || 0, e.y || 0, 0);
//         e.Label
//             ? ((this.tips_node.label_on.node.active = !0), (this.tips_node.label_on.string = e.Label))
//             : e.Sprite
//             ? ((this.tips_node.sprite_on.node.active = !0),
//               (this.tips_node.sprite_on.spriteFrame = e.Sprite),
//               this.tips_node.node_sprite_on_bg &&
//                   ((this.tips_node.node_sprite_on_bg.width = 1.3 * this.tips_node.sprite_on.node.width),
//                   (this.tips_node.node_sprite_on_bg.active = !0)))
//             : ((this.tips_node.label_on.node.active = !0), (this.tips_node.label_on.string = "点击手指"));
//     };
//     t.prototype.xianshi_shou = function (e) {
//         if (e.node) {
//             this.node_finger.active = !0;
//             var t = e.node.getBoundingBoxToWorld().center;
//             var n = t.x;
//             var o = t.y;
//             var i = this.node_mask.convertToNodeSpaceAR(cc.v3(n, o));
//             this.node_finger.position = i.add(cc.v3(e.offsetX || 0, e.offsetY || 0));
//         } else {
//             this.node_finger.position = cc.v3(e.x || 0, e.y || 0);
//         }
//     };
//     t.prototype._touch_callback = function () {
//         this._node_target.off("touchend", this._touch_callback, this);
//         if (this._node_callback) {
//             this._node_callback();
//         }
//     };
//     t.prototype.chufa_xiayige = function () {
//         var e = this._trigger_callback;
//         this._trigger_callback = null;
//         if (e) {
//             e();
//         }
//     };
//     t.prototype.xianshi_zhezhao_jiedian = function (e, t) {
//         this._trigger_callback = t.triggerCallback;
//         this._touch_bg_callback = t.onTouchBg;
//         t.touchMoveEnable ? (this._touchMoveEnabel = !0) : (this._touchMoveEnabel = !1);
//         if (e) {
//             this._node_target = e;
//             var n = t.offset || 0;
//             var o = e.getBoundingBoxToWorld();
//             var i = o.width;
//             var r = o.height;
//             var a = o.center;
//             var s = a.x;
//             var l = a.y;
//             if (t.callback) {
//                 e.off("touchend", this._touch_callback, this);
//                 e.on("touchend", this._touch_callback, this);
//                 this._node_callback = t.callback;
//             }
//             if (t.type === c.CIRCLE) {
//                 var u = Math.sqrt((i / 2) * (i / 2) + (r / 2) * (r / 2)) + n;
//                 var p = this.node_mask.convertToNodeSpaceAR(cc.v2(s, l));
//                 this.xianshi_zhezhao_yuan(new _(p.x, p.y, u), !1, t.effect);
//             } else {
//                 if (t.type === c.RECT) {
//                     var f = t.round || 0;
//                     var d = i + n;
//                     var h = r + n;
//                     var P = (p = this.node_mask.convertToNodeSpaceAR(cc.v2(s, l))).x - d / 2;
//                     var y = p.y - h / 2;
//                     this.xianshi_zhezhao_juxing(new g(P, y, d, h, f), !1, t.effect);
//                 }
//             }
//         } else {
//             if (t.type === c.CIRCLE) {
//                 var m = t.circle;
//                 if (m) {
//                     this.xianshi_zhezhao_yuan(m, !0, t.effect);
//                 }
//             } else {
//                 if (t.type === c.RECT) {
//                     var v = t.rect;
//                     if (v) {
//                         this.xianshi_zhezhao_juxing(v, !0, t.effect);
//                     }
//                 }
//             }
//         }
//     };
//     t.prototype.xianshi_zhezhao_yuan = function (e, t, n) {
//         if (void 0 === t) {
//             t = !1;
//         }
//         if (void 0 === n) {
//             n = s.NONE;
//         }
//         var o = e.x;
//         var i = e.y;
//         var r = e.radius;
//         this.node_mask.active = !0;
//         this.mask.drawCircle(o, i, cc.v2(r, r));
//         this._array_shape_list.push({
//             shape: e,
//             type: c.CIRCLE,
//             effect: n,
//             enableTouch: t
//         });
//         this.xianshi_texiao(n);
//     };
//     t.prototype.xianshi_zhezhao_juxing = function (e, t, n) {
//         if (void 0 === t) {
//             t = !1;
//         }
//         if (void 0 === n) {
//             n = s.NONE;
//         }
//         e.x;
//         e.y;
//         var o = e.width;
//         var i = e.height;
//         var r = e.mx;
//         var a = e.my;
//         var l = e.round;
//         this.node_mask.active = !0;
//         this.mask.clearDraw(!0);
//         this.mask.drawRoundRect(r, a, o, i, l);
//         this._array_shape_list.push({
//             shape: e,
//             type: c.RECT,
//             effect: n,
//             enableTouch: t
//         });
//         this.xianshi_texiao(n);
//     };
//     t.prototype.xianshi_texiao = function (e) {
//         var t = this;
//         if (e !== s.NONE && this._array_shape_list.length > 0) {
//             if (this._effect_action) {
//                 this._effect_action.stop();
//                 this._effect_action = null;
//             }
//             this.node_mask.v = 1.5;
//             var n = 0;
//             for (var o = this._array_shape_list; n < o.length; n++) {
//                 var i = o[n];
//                 if (i.type === c.CIRCLE) {
//                     var r = i.shape;
//                     if (e === s.SCALE) {
//                         r.suofang_dongzuo(this.mask, 1.5);
//                     }
//                 } else {
//                     if (i.type === c.RECT) {
//                         r = i.shape;
//                         if (e === s.SCALE) {
//                             r.suofang_dongzuo(this.mask, 1.5);
//                         }
//                     }
//                 }
//             }
//             cc.tween(this.node_mask)
//                 .to(
//                     0.3,
//                     {
//                         v: 1
//                     },
//                     {
//                         progress: function (n, o, i, r) {
//                             var a = n + (o - n) * r;
//                             t.mask.clearDraw();
//                             var l = 0;
//                             for (var u = t._array_shape_list; l < u.length; l++) {
//                                 var p = u[l];
//                                 if (p.type === c.CIRCLE) {
//                                     var f = p.shape;
//                                     if (e === s.SCALE) {
//                                         f.suofang_dongzuo(t.mask, a);
//                                     }
//                                 } else {
//                                     if (p.type === c.RECT) {
//                                         f = p.shape;
//                                         if (e === s.SCALE) {
//                                             f.suofang_dongzuo(t.mask, a);
//                                         }
//                                     }
//                                 }
//                             }
//                             return a;
//                         }
//                     }
//                 )
//                 .call(function () {
//                     t._effect_action = null;
//                 })
//                 .start();
//         }
//     };
//     t.ShapeType = c;
//     t.RangeEffect = s;
//     t.circle = P;
//     t.rect = y;
//     __decorate([h(cc.Node)], t.prototype, "node_finger", void 0);
//     __decorate([h(cc.Node)], t.prototype, "node_mask", void 0);
//     __decorate([h(AL_GAME_HUA_Tipsnode.default)], t.prototype, "tips_node", void 0);
//     return __decorate([d], t);
// })(cc.Component);
// exports.default = m;


const { ccclass, property } = cc._decorator;
const AL_GAME_HUA_Tipsnode = require("./AL_GAME_HUA_Tipsnode");

/** @enum {number} RangeEffect 范围效果枚举 */
const RangeEffect = {
    NONE: 0,
    SCALE: 1
};

/** @enum {number} ShapeType 形状类型枚举 */
const ShapeType = {
    CIRCLE: 0,
    RECT: 1,
    POLYGON: 2
};

/** 
 * 用于表示圆形的数据结构
 * @class CircleShape
 */
class CircleShape {
    constructor(x, y, radius) {
        this.x = x;
        this.y = y;
        this.radius = radius;
    }

    /**
     * 对圆形进行缩放绘制
     * @param {cc.Graphics} graphics 
     * @param {number} factor 缩放因子
     */
    suofang_dongzuo(graphics, factor) {
        const scaledRadius = this.radius * factor;
        graphics.drawCircle(this.x, this.y, cc.v2(scaledRadius, scaledRadius));
    }
}

/**
 * @param {number} x
 * @param {number} y
 * @param {number} radius
 * @returns {CircleShape}
 */
function chuangjian_yuan(x, y, radius) {
    return new CircleShape(x, y, radius);
}

/** 
 * 用于表示矩形的数据结构
 * @class RectShape
 * @extends {cc.Component}
 */
class RectShape {
    constructor(mx, my, width, height, round) {
        this.mx = mx;
        this.my = my;
        this.round = round;
        this.width = width;
        this.height = height;
        this.x = mx + width / 2;
        this.y = my + height / 2;
    }

    /**
     * 对矩形进行缩放绘制
     * @param {cc.Graphics} graphics
     * @param {number} factor 缩放因子
     */
    suofang_dongzuo(graphics, factor) {
        const scaledWidth = this.width * factor;
        const scaledHeight = this.height * factor;
        const startX = this.x - scaledWidth / 2;
        const startY = this.y - scaledHeight / 2;
        graphics.drawRoundRect(startX, startY, scaledWidth, scaledHeight, this.round);
    }

    /**
     * 获取矩形的多边形顶点
     * @returns {Array<cc.Vec2>}
     */
    getPolygon() {
        return [
            cc.v2(this.mx, this.my),
            cc.v2(this.mx, this.my + this.height),
            cc.v2(this.mx + this.width, this.my + this.height),
            cc.v2(this.mx + this.width, this.my)
        ];
    }
}

/**
 * @param {number} mx
 * @param {number} my
 * @param {number} width
 * @param {number} height
 * @param {number} [round=0]
 * @returns {RectShape}
 */
function chuangjian_juxing(mx, my, width, height, round = 0) {
    return new RectShape(mx, my, width, height, round);
}

/**
 * 提供一些碰撞区域操作的组件类
 * @subpackage
 * @class MaskHandler
 * @extends {cc.Component}
 */
@ccclass
class MaskHandler extends cc.Component {
    @property(cc.Node)
    node_finger = null;

    @property(cc.Node)
    node_mask = null;

    @property(AL_GAME_HUA_Tipsnode.default)
    tips_node = null;

    /** @type {cc.Node} */
    _node_target = null;

    /** @type {Array} */
    _array_shape_list = [];

    /** @type {Function|null} */
    _node_callback = null;

    _touch_move_enable = false;
    /** @type {Function|null} */
    _trigger_callback = null;

    /** @type {Function|null} */
    _touch_bg_callback = null;

    _has_touch_start = false;
    /** @type {cc.Mask} */
    mask = null;

    /** @type {cc.Tween} */
    _effect_action = null;

    onLoad() {
        this.mask = this.node_mask.getComponent(cc.Mask);
        this.mask._updateGraphics = function () {};
        this.mask.node.targetOff(this.mask);
        this.mask.clearDraw();
        this.tips_node.node.active = false;
        this.node_finger.active = false;
        this.node.on("touchstart", this.handle_event_touch_start, this);
        this.node.on("touchmove", this.handle_event_touch_move, this);
        this.node.on("touchend", this.handle_event_touch_end, this);
    }

    onDestroy() {
        this.node.off("touchstart", this.handle_event_touch_start, this);
        this.node.off("touchmove", this.handle_event_touch_move, this);
        this.node.off("touchend", this.handle_event_touch_end, this);
    }

    jiancha_chumo_shijian(e) {
        if (!this._touch_bg_callback) {
            if (this._node_target) {
                this._node_target._hitTest(e.getLocation())
                    ? this.node.setSwallowTouches(false)
                    : this.node.setSwallowTouches(true);
            } else if (this._array_shape_list.length > 0) {
                for (const shapeConfig of this._array_shape_list) {
                    if (shapeConfig.enableTouch) {
                        const localPoint = this.node_mask.convertToNodeSpaceAR(e.getLocation());
                        if (shapeConfig.type === ShapeType.RECT && cc.Intersection.pointInPolygon(localPoint, shapeConfig.shape.getPolygon())) {
                            this.node.setSwallowTouches(false);
                            return;
                        }
                        if (shapeConfig.type === ShapeType.CIRCLE && localPoint.sub(cc.v2(shapeConfig.shape.x, shapeConfig.shape.y)).magSqr() <= shapeConfig.shape.radius * shapeConfig.shape.radius) {
                            this.node.setSwallowTouches(false);
                            return;
                        }
                    }
                }
                this.node.setSwallowTouches(true);
            }
        }
    }

    handle_event_touch_start(e) {
        this._has_touch_start = true;
        this.jiancha_chumo_shijian(e);
    }

    handle_event_touch_move(e) {
        if (this._has_touch_start) {
            this._touch_move_enable ? this.jiancha_chumo_shijian(e) : this.node.setSwallowTouches(true);
        }
    }

    handle_event_touch_end(e) {
        if (this._has_touch_start) {
            if (this._touch_bg_callback) {
                this._touch_bg_callback();
            }
            this.jiancha_chumo_shijian(e);
            this._has_touch_start = false;
        }
    }

    clear() {
        if (this.node_finger) {
            this.node_finger.active = false;
        }
        this.node.setSwallowTouches(true);
        this._node_target = null;
        this._array_shape_list = [];
        this._touch_bg_callback = null;
        this.node_mask.active = false;
        this.tips_node.node.active = false;
        this._has_touch_start = false;
    }

    xianshi_node_tips(config) {
        this.tips_node.node.active = true;
        this.tips_node.node.position = cc.v3(config.x || 0, config.y || 0, 0);
        if (config.Label) {
            this.tips_node.label_on.node.active = true;
            this.tips_node.label_on.string = config.Label;
        } else if (config.Sprite) {
            this.tips_node.sprite_on.node.active = true;
            this.tips_node.sprite_on.spriteFrame = config.Sprite;
            if (this.tips_node.node_sprite_on_bg) {
                this.tips_node.node_sprite_on_bg.width = 1.3 * this.tips_node.sprite_on.node.width;
                this.tips_node.node_sprite_on_bg.active = true;
            }
        } else {
            this.tips_node.label_on.node.active = true;
            this.tips_node.label_on.string = "点击手指";
        }
    }

    xianshi_shou(locationConfig) {
        this.node_finger.active = true;
        if (locationConfig.node) {
            const worldCenter = locationConfig.node.getBoundingBoxToWorld().center;
            const localPosition = this.node_mask.convertToNodeSpaceAR(cc.v3(worldCenter.x, worldCenter.y));
            this.node_finger.position = localPosition.add(cc.v3(locationConfig.offsetX || 0, locationConfig.offsetY || 0));
        } else {
            this.node_finger.position = cc.v3(locationConfig.x || 0, locationConfig.y || 0);
        }
    }

    _touch_callback() {
        this._node_target.off("touchend", this._touch_callback, this);
        if (this._node_callback) {
            this._node_callback();
        }
    }

    chufa_xiayige() {
        const callback = this._trigger_callback;
        this._trigger_callback = null;
        if (callback) {
            callback();
        }
    }

    xianshi_zhezhao_jiedian(targetNode, config) {
        this._trigger_callback = config.triggerCallback;
        this._touch_bg_callback = config.onTouchBg;
        this._touch_move_enable = config.touchMoveEnable ? true : false;

        if (targetNode) {
            this._node_target = targetNode;
            const offset = config.offset || 0;
            const boundingBox = targetNode.getBoundingBoxToWorld();
            const { width, height, center } = boundingBox;

            const rectAttributes = {
                mx: center.x - width / 2,
                my: center.y - height / 2,
                width: width + offset,
                height: height + offset,
                round: config.round || 0
            };

            if (config.callback) {
                targetNode.off("touchend", this._touch_callback, this);
                targetNode.on("touchend", this._touch_callback, this);
                this._node_callback = config.callback;
            }

            const localPosition = this.node_mask.convertToNodeSpaceAR(cc.v2(center.x, center.y));
            if (config.type === ShapeType.CIRCLE) {
                const radius = Math.sqrt((width / 2) ** 2 + (height / 2) ** 2) + offset;
                this.xianshi_zhezhao_yuan(new CircleShape(localPosition.x, localPosition.y, radius), false, config.effect);
            } else if (config.type === ShapeType.RECT) {
                const rectShape = new RectShape(rectAttributes.mx, rectAttributes.my, rectAttributes.width, rectAttributes.height, rectAttributes.round);
                this.xianshi_zhezhao_juxing(rectShape, false, config.effect);
            }
        } else {
            if (config.type === ShapeType.CIRCLE && config.circle) {
                this.xianshi_zhezhao_yuan(config.circle, true, config.effect);
            } else if (config.type === ShapeType.RECT && config.rect) {
                this.xianshi_zhezhao_juxing(config.rect, true, config.effect);
            }
        }
    }

    xianshi_zhezhao_yuan(circleShape, enableTouch = false, effect = RangeEffect.NONE) {
        const { x, y, radius } = circleShape;
        this.node_mask.active = true;
        this.mask.drawCircle(x, y, cc.v2(radius, radius));

        this._array_shape_list.push({
            shape: circleShape,
            type: ShapeType.CIRCLE,
            effect,
            enableTouch
        });

        this.xianshi_texiao(effect);
    }

    xianshi_zhezhao_juxing(rectShape, enableTouch = false, effect = RangeEffect.NONE) {
        const { x, y, width, height, round } = rectShape;
        this.node_mask.active = true;
        this.mask.clearDraw(true);
        this.mask.drawRoundRect(x, y, width, height, round);

        this._array_shape_list.push({
            shape: rectShape,
            type: ShapeType.RECT,
            effect,
            enableTouch
        });

        this.xianshi_texiao(effect);
    }

    xianshi_texiao(effect) {
        if (effect !== RangeEffect.NONE && this._array_shape_list.length > 0 && !this._effect_action) {
            this.node_mask.v = 1.5;
            let progressValue;

            const drawAction = (interpolatedValue) => {
                this.mask.clearDraw();
                for (const shapeConfig of this._array_shape_list) {
                    if (shapeConfig.effect === RangeEffect.SCALE) {
                        shapeConfig.shape.suofang_dongzuo(this.mask, interpolatedValue);
                    }
                }
                return interpolatedValue;
            };

            this._effect_action = cc.tween(this.node_mask)
                .to(0.3, { v: 1 }, { progress: (start, end, current, time) => {
                        progressValue = start + (end - start) * time;
                        return drawAction(progressValue);
                }})
                .call(() => { this._effect_action = null; })
                .start();
        }
    }

    static ShapeType = ShapeType;
    static RangeEffect = RangeEffect;
    static circle = chuangjian_yuan;
    static rect = chuangjian_juxing;
}
exports.default = MaskHandler;